//
//  AuditProcessView.swift
//  AoNongOA
//
//  Created by zwb on 16/8/5.
//  Copyright © 2025 aowang. All rights reserved.
//

import UIKit





class AuditProcessPointV: UIView {
    
    
    private var points = [TodopProgress]()
    // 审核信息文字
    private lazy var auditLabel: UILabel = {
        
        let label = UILabel()
        label.text = "审核信息"
        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor = .black
        return label
    }()
    lazy var historyBtn: UIButton = {
        let label = UIButton()
        label.setTitle("[历史]", for: .normal)
        label.setTitleColor(.black, for: .normal)
        label.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        return label
    }()
    
    // 审核信息图标
    private  lazy var iconView:UIImageView = {
        let v = UIImageView(image:UIImage(named:"af_arrow_down"))
        return v
    }()
 
    private lazy var line:UIView = {
        let v = UIView()
        v.backgroundColor = Theme.TABLEVIEW_COLOR
        return v
    }()
    
    var btnType:OperationPerm?
    
    
    
    lazy  var auditTopBg: UIView = {
        let  v = UIView()
        v.backgroundColor = .white
        v.layer.masksToBounds = true
        v.layer.cornerRadius = 5
        return v
    }()
    
  
   
    
    lazy var timeTableV:PointProcessView = {
        let v = PointProcessView()
        return v
    }()
   
  
   
    var appr = AuditProcessAppear()
  
    var parVc:UIViewController?
   
    convenience init(points: [TodopProgress], btnType:OperationPerm?,title: String,appr:AuditProcessAppear? = nil,vc:UIViewController? = nil) {
        self.init(points: points,btnType:btnType,appr: appr,vc: vc)
        self.auditLabel.text = title
    }
    
    init(points: [TodopProgress],btnType:OperationPerm?,appr:AuditProcessAppear? = nil,vc:UIViewController? = nil) {
        super.init(frame: CGRect(x: 0, y: 0, width: KSGlobal.screenW, height: 480))
        
        self.points = points
        self.btnType = btnType
 
        self.parVc = vc
        if appr.isSome {
            self.appr = appr!
        }
        
        createView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var folding = false
    var timelineHeight:[CGFloat] = []
    var tLtotalHeight:CGFloat = 0
    
    
    fileprivate func createView() {
        
 
        
        
        // 计算每个审核节点的高度
        for item in points {
            var tHH:CGFloat = 0
            tHH += CGFloat(70)
            
            for user in item.userinfo {
                
                
                
                for iii in user.comment {
                    //计算审批文字内容高度
                    var hhh = iii.comment.unwrap.heightWithStringAttributes(attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15)], fixedWidth: KSGlobal.screenW - 70)
                    if hhh < 25 {
                        hhh = 25
                    }
                    
                    tHH += hhh
                }
                
                
                
                // 加上附件高度
                tHH += CGFloat(user.attachments.count * 25)
            }
            self.timelineHeight.append(tHH)
        }
        self.tLtotalHeight = self.timelineHeight.reduce(0, +)
        
        
        self.backgroundColor = UIColor(kshex:"#f5f5f5")
        self.addSubview(auditTopBg)
        let tap = UITapGestureRecognizer()
        tap.numberOfTapsRequired = 1
        tap.addTap { [unowned self] _ in
            folding = !folding
            self.makeConstra()
            
        }
        auditTopBg.addGestureRecognizer(tap)
        
        
        auditTopBg.addSubview(iconView)
        auditTopBg.addSubview(auditLabel)
        if !checkPlatform(type: .zhuYouAn) {
            auditTopBg.addSubview(historyBtn)
        }
        auditTopBg.addSubview(line)
        
        timeTableV.points = points
        timeTableV.timelineHeight = timelineHeight
        timeTableV.vc = self.parVc
        addSubview(timeTableV)
        
      
        
        self.makeConstra()
        
    }
    
  
    // 布局
    func makeConstra() {
        
       
        var ttH:CGFloat = 0
         ttH += 85
        
        
        
        if folding {
            self.ks_height = ttH
            iconView.image = UIImage(named: "af_arrow_right")
            timeTableV.isHidden = true
        } else {
            self.ks_height = ttH + self.tLtotalHeight
            iconView.image = UIImage(named: "af_arrow_down")
            timeTableV.isHidden = false
            timeTableV.snp.makeConstraints { (make) in
                make.top.equalTo(line.snp.bottom)
                make.left.equalTo(self).offset(5)
                make.right.equalTo(self).offset(-5)
                make.height.equalTo(self.tLtotalHeight)
            }
        }
        
        auditTopBg.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(5)
            make.right.equalTo(self).offset(-5)
            make.height.equalTo(40)
            make.top.equalTo(self).offset(5)
        }
        
        auditLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(auditTopBg)
            make.height.equalTo(35)
            make.centerY.equalTo(auditTopBg)
        }
        
        
        iconView.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 16, height: 16))
            make.centerY.equalTo(auditTopBg)
            make.right.equalTo(auditTopBg).offset(-8)
        }
        
        if !checkPlatform(type: .zhuYouAn) {
            historyBtn.snp.makeConstraints { (make) in
                make.size.equalTo(CGSize(width: 55, height: 30))
                make.centerY.equalTo(auditTopBg)
                make.left.equalTo(auditTopBg).offset(8)
            }
        }
        
        line.snp.makeConstraints { (make) in
            make.bottom.equalTo(auditTopBg)
            make.height.equalTo(1)
            make.left.equalTo(auditTopBg).offset(8)
            make.right.equalTo(auditTopBg).offset(-8)
        }
        
 
        
        
    }

}

 
